package com.cjj.proxy.cglib;

import com.cjj.proxy.House;
import com.cjj.proxy.Xiaoming;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName Cglib
 * @Date 2021/08/08 16:11:26
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class CglibProxy implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("我是房产中介，使用动态代理技术开始监听...");
        Object invokeSuper = methodProxy.invokeSuper(o, objects);
        System.out.println("我是房产中介，使用动态代理技术结束监听...");
        return invokeSuper;
    }

    public static void main(String[] args) {
        CglibProxy cglibProxy = new CglibProxy();
        // 动态代理使用asm框架生产代理
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Xiaoming.class);
        enhancer.setCallback(cglibProxy);
        House house = (House) enhancer.create();
        house.mai();
    }
}
